#!/bin/bash

MAKE_FLAG="" #-j12

STL_PORT="false"
if [ $STL_PORT = "true" ]; then
	STL_PATH="$PWD/../../libs/STLport-5.2.1/"
	STL_DEFINE="STL_PORT;STDEXT=std"
	STL_INCS="$STL_PATH/include/stlport"
	STL_LIBS="-L$STL_PATH/lib/ -lstlport"
else
	STL_DEFINE="NO_STL_PORT"
	STL_INCS="."
	STL_LIBS=""
fi

SIGNAL_HANDLER="false"
if [ $SIGNAL_HANDLER = "true" ]; then
	SIGNAL_HANDLER_DEFINE="ENABLE_SIGNAL_HANDLER=1"
else
	SIGNAL_HANDLER_DEFINE="ENABLE_SIGNAL_HANDLER=0"
fi

GOOGLE_PPROF="false"
if [ $GOOGLE_PPROF = "true" ]; then
	GOOGLE_PPROF_DEFINE="USE_GOOGLE_PPROF=1;ENABLE_PROFILER=1"
	GOOGLE_PPROF_LIBS="-ltcmalloc -lprofiler"
else
	GOOGLE_PPROF_DEFINE="USE_GOOGLE_PPROF=0;ENABLE_PROFILER=0"
	GOOGLE_PPROF_LIBS=""
fi

DEPEND_LIB=""


PROJECT_DIR="./win32"
#=======================================share Define===============
SHARE_DIR="./win32/share"
SHARE_VCPROJ="./win32/share/share.vcproj"
SHARE_INCS="$STL_INCS;../../../source/share;../../../source/entities"
SHARE_MACROS="$STL_DEFINE;$SIGNAL_HANDLER_DEFINE;$GOOGLE_PPROF_DEFINE"
SHARE_TARGET="share.a"

#=======================================entities Define===============
ENTITIES_DIR="./win32/entities"
ENTITIES_VCPROJ="./win32/entities/entities.vcproj"
ENTITIES_INCS="$STL_INCS;../../../source/share;../../../source/entities"
ENTITIES_MACROS="$STL_DEFINE;$SIGNAL_HANDLER_DEFINE;$GOOGLE_PPROF_DEFINE"
ENTITIES_TARGET="entities.a"

#=======================================level1 Define===============
LEVEL1_DIR="./win32/level1"
LEVEL1_VCPROJ="./win32/level1/level1.vcproj"
LEVEL1_INCS="$STL_INCS;../../../source/share;../../../source/entities"
LEVEL1_MACROS="$STL_DEFINE;$SIGNAL_HANDLER_DEFINE;$GOOGLE_PPROF_DEFINE"
LEVEL1_TARGET="level1.a"

$oper
while [ "$oper" != "exit" ]; do
	echo "Input a build command[makefile/clean/build/link] :"
	read oper

	if [ "$oper" = "makefile" ]
	then
		python convert2MK.py $SHARE_VCPROJ $SHARE_TARGET static $SHARE_INCS $SHARE_MACROS "./" > "$SHARE_DIR/share.make"
		python convert2MK.py $ENTITIES_VCPROJ $ENTITIES_TARGET static $ENTITIES_INCS $ENTITIES_MACROS "./" > "$ENTITIES_DIR/entities.make"
		python convert2MK.py $LEVEL1_VCPROJ $LEVEL1_TARGET static $LEVEL1_INCS $LEVEL1_MACROS "./" > "$LEVEL1_DIR/level1.make"
	elif [ "$oper" = "clean stlport" ]; then
		if [ $STL_PORT = "true" ]; then
			cd $STL_PATH
			make clean
			cd $OLDPWD
		fi

	elif [ "$oper" = "clean" ]; then
		cd $SHARE_DIR 
		make clean -f share.make
		cd $OLDPWD 
		
		cd $ENTITIES_DIR 
		make clean -f entities.make
		cd $OLDPWD 
		
		cd $LEVEL1_DIR 
		make clean -f level1.make
		cd $OLDPWD 

	elif [ "$oper" = "build" ]; then
		
		if [ $STL_PORT = "true" ]; then
			cd $STL_PATH
			sh ./configure --prefix=$STL_PATH --disable-shared --enable-static --without-debug --without-stldebug --with-extra-cxxflags='-lpthread -fPIC -O2 -fno-builtin-memcpy -fno-builtin-memmove' --with-extra-cflags='-lpthread -O2 -fPIC -fno-builtin-memcpy -fno-builtin-memmove'
			make install
			cd $OLDPWD
		fi
		echo $OLDPWD
		echo "building io library ..."
		cd  $SHARE_DIR 
		make $MAKE_FLAG -f share.make
		cp -f $SHARE_TARGET $OLDPWD
		cd $OLDPWD 
		
		cd  $ENTITIES_DIR 
		make $MAKE_FLAG -f entities.make
		cp -f $ENTITIES_TARGET $OLDPWD
		cd $OLDPWD 
		
		cd  $LEVEL1_DIR 
		make $MAKE_FLAG -f level1.make
		cp -f $LEVEL1_TARGET $OLDPWD
		cd $OLDPWD 
		
		echo $OLDPWD
		echo "build done!"

		echo "linking ..."
		LINK_CMD="arm-linux-g++ -fPIC $LEVEL1_TARGET $ENTITIES_TARGET $SHARE_TARGET $DEPEND_LIB -o level1 -Wl,-Bstatic $STL_LIBS -lm  -lz -Wl,-Bdynamic -lrt -lpthread $GOOGLE_PPROF_LIBS -rdynamic"
		echo $LINK_CMD
		$LINK_CMD

		echo "copy target"
		cp level1 ../bin/level1
		echo "link done!"
	else
		echo "Input command: makefile/clean/build/link"
	fi

done

exit 0
